<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品评论</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.css">
</head>
<body>

<div id="waypoint" class="m-margin- animated fadeIn">
    <div class="ui container m-opacity box-shadow-max">
        <div  class="ui bottom attached segment">
            <!--评论区域列表-->
            <div id="comment-container"  class="ui teal segment">
                <div th:fragment="commentList">
                    <div class="ui threaded comments" style="max-width: 100%;">
                        <h3 class="ui dividing header">商品评论</h3>
                        <div class="comment" th:each="comment : ${comments}">
                            <div class="content">
                                <a class="author" >
                                    <span th:text="${comment.userName}">小红</span>
                                </a>
                                <div class="metadata">
                                    <span class="date" th:text="${#dates.format(comment.ccreateTime,'yyyy-MM-dd HH:mm')}">Today at 5:42PM</span>
                                </div>
                                <div class="text" th:text="${comment.comment}">
                                    愿你走出半生，归来仍是少年！
                                </div>
                                <div class="actions">
                                    <a class="reply" data-commentid="1" data-commentnickname="Matt" th:attr="data-commentid=${comment.cId},data-commentnickname=${comment.userName}" onclick="reply(this)">回复</a>
                                </div>
                            </div>
                            <!--子集评论-->
                            <div class="comments" th:if="${#arrays.length(comment.replyComments)}>0">
                                <div class="comment" th:each="reply : ${comment.replyComments}">
                                    <div class="content">
                                        <a class="author" >
                                            <span th:text="${reply.userName}">小白</span>
                                            &nbsp;<span th:text="|@ ${reply.parentNickname}|" class="m-teal">@ 小红</span>
                                        </a>
                                        <div class="metadata">
                                            <span class="date" th:text="${#dates.format(reply.ccreateTime,'yyyy-MM-dd HH:mm')}">Today at 5:42PM</span>
                                        </div>
                                        <div class="text" th:text="${reply.comment}">
                                            你也是！
                                        </div>
                                        <div class="actions">
                                            <a class="reply" data-commentid="1" data-commentnickname="Matt" th:attr="data-commentid=${reply.cId},data-commentnickname=${reply.userName}" onclick="reply(this)">回复</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
            <div id="comment-form" class="ui form">
                <input type="hidden" name="parentComment.id" value="-1">
                <div class="field">
                    <textarea name="content" placeholder="请输入评论信息..."></textarea>
                </div>
                <div class="fields">
                    <div class="field m-mobile-wide m-margin-bottom-small">
                    </div>
                    <div class="field m-mobile-wide m-margin-bottom-small">
                    </div>
                    <div class="field  m-margin-bottom-small m-mobile-wide">
                        <button id="commentpost-btn" type="button" class="ui teal button m-mobile-wide"><i class="edit icon"></i>发布</button>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.js"></script>

<script th:inline="javascript">

    var jiajuId = localStorage.getItem("jiajuId");
    var userId = localStorage.getItem("userId")
        //评论表单验证
        $('.ui.form').form({
            fields: {
                title: {
                    identifier: 'content',
                    rules: [{
                        type: 'empty',
                        prompt: '请输入评论内容'
                    }
                    ]
                }

            }
        });


        $(document).ready(function () {

            $("#comment-container").load("/com/comment?jiajuId="+jiajuId);
        });

        $('#commentpost-btn').click(function () {
            var boo = $('.ui.form').form('validate form');
            if (boo) {
                console.log('校验成功');
                postData();
            } else {
                console.log('校验失败');
            }
        });

        //post格式
        //$('#message').load("AJAX?name=" + userName,{"Content-type":"application/x-www-form-urlencoded"});

        function postData() {

            $("#comment-container").load("/com/comment", {
                'parentId': $("[name='parentComment.id']").val(),
                /*"userName": $("[name='nickname']").val(),*/
                'comments': $("[name='content']").val(),
                'userId': userId,
                'jiajuId': jiajuId
            }, function (res) {
                // $(window).scrollTo($('#goto'),0);
                if (res.code != 0) {
                    alert("评论成功！");
                    window.location.reload();//刷新
                }
                // console.log("1111111");
                clearContent();
            });
        }

        function clearContent() {
            $("[name='nickname']").val('');
            $("[name='content']").val('');
            $("[name='parentComment.id']").val(-1);
            $("[name='content']").attr("placeholder", "请输入评论信息...");
        }

        function reply(obj) {
            var commentId = $(obj).data('commentid');
            var commentNickname = $(obj).data('commentnickname');
            $("[name='content']").attr("placeholder", "@" + commentNickname).focus();
            $("[name='parentComment.id']").val(commentId);
            $(window).scrollTop($('#comment-form'), 500);
        }

</script>
</body>
</html>